package com.utf999.qiangmanger.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.utf999.qiangmanger.controller.permission.AuthorityAnnotation;
import com.utf999.qiangmanger.controller.permission.AuthorityScope;
import com.utf999.qiangmanger.controller.permission.PromptModeType;
import com.utf999.qiangmanger.entity.JsonEntity;
import com.utf999.qiangmanger.entity.orm.AdminUserEntity;
import com.utf999.qiangmanger.exception.AuthenticationException;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;
import com.utf999.qiangmanger.service.iface.IAdminUserService;

/**
 * @author yinwenjie
 */
@Controller
@RequestMapping(value={"/adminuser"})
public class AdminUserController extends BaseController {
	
	private static Log controllerLog4j = LogFactory.getLog(AdminUserController.class);
	
	@Autowired
	private IAdminUserService adminUserService;
	
	/**
	 * 该处理方法用于新增一个后台管理员/后台使用者
	 * @param request
	 * @param response
	 * @param username 新增的用户（注意如果用户名重复，会报错）
	 * @param password 新增用户的密码信息
	 * @param adminlevel 管理员用户等级：1、超级管理员；2、门店管理员
	 * @param companyid 这个管理员如果是公司级别的管理员，就需要传入对应的公司ID
	 * @param storeid 这个管理员所说的店面信息（这样就可以查找出公司信息了）<br>
	 * 注意，如果是超级管理员，就不用传入storeid。传入了也没用
	 * @throws BizException
	 */
	@RequestMapping(value={"/appendAdminuser"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin,AuthorityScope.CompanyAdmin,AuthorityScope.StoreAdmin} , promptMode=PromptModeType.AJAX)
	public void appendAdminuser(HttpServletRequest request , HttpServletResponse response , String username , String password , Integer adminlevel, String companyid , String storeid ) throws BizException {
		JsonEntity jsonEntity = new JsonEntity();
		
		AdminUserEntity user = null;
		try {
			if(adminlevel == 1) {
				user = this.adminUserService.appendSuperAdminuser(username, password);
			} else if(adminlevel == 2) {
				user = this.adminUserService.appendCompanyAdminuser(username, password, companyid);
			} else if(adminlevel == 3 || adminlevel == 4) {
				user = this.adminUserService.appendStoreAdminuser(username, password, adminlevel, storeid);
			}
			
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(user);
			this.writeResponseMsg(response, jsonEntity , new String[]{"storefront","company"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} catch (Exception e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(ResponseCode.E501);
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
	
	/**
	 * 修改指定管理员的密码
	 * @param request 
	 * @param response 
	 * @param adminid 指定的用户编号
	 * @param password 新的密码
	 */
	@RequestMapping(value={"/modifyAdminuserPassword"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin,AuthorityScope.CompanyAdmin,AuthorityScope.StoreAdmin} , promptMode=PromptModeType.AJAX)
	public void modifyAdminuserPassword(HttpServletRequest request , HttpServletResponse response , String adminid , String password) {
		JsonEntity jsonEntity = new JsonEntity();
		
		AdminUserEntity user = null;
		try {
			this.adminUserService.modifyAdminuserPassword(adminid, password);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(user);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 修改用户的状态
	 * @param request
	 * @param response
	 * @param adminid 指定的管理员id
	 */
	@RequestMapping(value={"/modifyUserStatus"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin,AuthorityScope.CompanyAdmin,AuthorityScope.StoreAdmin} , promptMode=PromptModeType.AJAX)
	public void modifyUserStatus(HttpServletRequest request , HttpServletResponse response , String adminid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		AdminUserEntity user = null;
		try {
			this.adminUserService.modifyUserStatus(adminid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(user);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 这是一个准备页面。跳转到”后台用户的管理页面“。<br>
	 * 注意这里需要查询得到当前使用者的身份。<br>
	 * 该页面需要登录才能进入
	 * @param request 
	 * @param response 
	 * @return 
	 */
	@RequestMapping(value={"/adminuserList"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin,AuthorityScope.CompanyAdmin,AuthorityScope.StoreAdmin} , promptMode=PromptModeType.REDIRECT)
	public ModelAndView adminuserList(HttpServletRequest request , HttpServletResponse response) throws AuthenticationException {
		return new ModelAndView("/configuration/adminuserlist");
	}
	
	/**
	 * 查询指定商户下的后台管理员信息，并使用ajax的方式返回（包括店面管理员和超市收银员两种）
	 * @param storeid 商户的编号信息
	 * @param status 可能传入的参数，如果传入，则表示只查询某种状态的管理员用户 
	 * @return 符合查询条件的信息在这里返回
	 */
	@RequestMapping(value={"/queryStoreAdmins"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin,AuthorityScope.CompanyAdmin,AuthorityScope.StoreAdmin} , promptMode=PromptModeType.AJAX)
	public void queryStoreAdmins(HttpServletRequest request , HttpServletResponse response , String storeid , Integer status) {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<AdminUserEntity> users = null;
		try {
			users = this.adminUserService.queryAdminByStore(storeid , status);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(users);
			this.writeResponseMsg(response, jsonEntity , new String[]{"storefront","company"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
	
	/**
	 * 查询系统中指定公司的公司级管理员的信息，并使用ajax的方式进行返回
	 * @param request
	 * @param response
	 * @param companyid 指定的公司编号
	 * @param status 可能传入的参数，如果传入，则表示只查询某种状态的管理员用户 
	 */
	@RequestMapping(value={"/queryCompanyAdmins"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin,AuthorityScope.CompanyAdmin,AuthorityScope.StoreAdmin} , promptMode=PromptModeType.AJAX)
	public void queryCompanyAdmins(HttpServletRequest request , HttpServletResponse response, String companyid, Integer status) throws BizException {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<AdminUserEntity> users = null;
		try {
			users = this.adminUserService.queryCompanyAdmin(companyid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(users);
			this.writeResponseMsg(response, jsonEntity , new String[]{"storefront","company"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
	
	/**
	 * 查询系统中超级管理员的信息，并使用ajax的方式进行返回
	 * @param request
	 * @param response
	 * @param status 可能传入的参数，如果传入，则表示只查询某种状态的管理员用户 
	 */
	@RequestMapping(value={"/querySuperAdmins"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin,AuthorityScope.CompanyAdmin,AuthorityScope.StoreAdmin} , promptMode=PromptModeType.AJAX)
	public void querySuperAdmins(HttpServletRequest request , HttpServletResponse response, Integer status) throws BizException {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<AdminUserEntity> users = null;
		try {
			users = this.adminUserService.querySuperAdmin(status);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(users);
			this.writeResponseMsg(response, jsonEntity , new String[]{"storefront","company"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
	
	/**
	 * 使用管理员用户名查询指定的管理员信息。一般这个方法可以用户判断指定的管理员用户名是否已被使用
	 * @param request
	 * @param response
	 * @param userName 指定的用户名信息
	 * @throws BizException
	 */
	@RequestMapping(value={"/queryAdminUser"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin,AuthorityScope.CompanyAdmin,AuthorityScope.StoreAdmin} , promptMode=PromptModeType.AJAX)
	public void queryAdminUser(HttpServletRequest request , HttpServletResponse response , String userName) throws BizException {
		JsonEntity jsonEntity = new JsonEntity();
		
		AdminUserEntity user = null;
		try {
			user = this.adminUserService.queryAdminUser(userName);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(user);
			this.writeResponseMsg(response, jsonEntity , new String[]{"storefront","company"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
	
	/**
	 * 使用管理员用户名查询指定的管理员信息。一般这个方法可以用户判断指定的管理员用户名是否已被使用
	 * @param request
	 * @param response
	 * @param userName 指定的用户名信息
	 * @throws BizException
	 */
	@RequestMapping(value={"/exsitAdminUser"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin,AuthorityScope.CompanyAdmin,AuthorityScope.StoreAdmin} , promptMode=PromptModeType.AJAX)
	public void exsitAdminUser(HttpServletRequest request , HttpServletResponse response , String userName) throws BizException {
		JsonEntity jsonEntity = new JsonEntity();
		
		AdminUserEntity user = null;
		try {
			user = this.adminUserService.queryAdminUser(userName);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			//如果存在，就返回true；其他情况返回
			if(user != null) {
				jsonEntity.setData(true);
			} else {
				jsonEntity.setData(false);
			}
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
}